# Contributor: Prokop Randáček <prokop@rdck.dev>
# Maintainer: Prokop Randáček <prokop@rdck.dev>
pkgname=stockfish
pkgver=16
_nnue="5af11540bbfe"
pkgrel=0
pkgdesc="Strong chess engine"
url="https://stockfishchess.org/"
# s390x: doesn't build
# ppc64le: fails tests
arch="all !ppc64le !s390x"
license="GPL-3.0-only"
checkdepends="bash expect"
source="$pkgname-$pkgver.tar.gz::https://github.com/official-stockfish/Stockfish/archive/refs/tags/sf_$pkgver.tar.gz
	https://tests.stockfishchess.org/api/nn/nn-$_nnue.nnue
	no-lto.patch
	"
builddir="$srcdir/Stockfish-sf_$pkgver/src"

prepare() {
	default_prepare

	ln -sfv "$srcdir"/nn-$_nnue.nnue "$builddir"/
}

build() {
	export CXXFLAGS="$CXXFLAGS -O2"
	export CPPFLAGS="$CPPFLAGS -O2"

	local arch
	case "$CARCH" in
	x86_64) arch=x86-64 ;;
	x86) arch=x86-32 ;;
	aarch64) arch=armv8 ;;
	armhf) arch=armv7 ;;
	ppc64le) arch=ppc-64 ;;
	armv7) arch=armv7 ;;
	riscv64) arch=riscv64 ;;
	*) die "Unable to determine architecture from CARCH=$CARCH" ;;
	esac

	make COMP="${CC:-cc}" ARCH="$arch" profile-build
}

check() {
	# not supported on all platforms that stockfish supports
	# ../tests/instrumented.sh --valgrind
	# ../tests/instrumented.sh --valgrind-thread
	# requires building with sanitizers
	# ../tests/instrumented.sh --sanitizer-undefined
	# ../tests/instrumented.sh --sanitizer-thread
	../tests/instrumented.sh
	../tests/perft.sh
	../tests/reprosearch.sh
}

package() {
	install -Dm755 stockfish "$pkgdir"/usr/bin/stockfish
}

sha512sums="
cc8728bd76cb71ab2e3dddb909acf3ebd26a7d44130509dcb2cd90959c939d7324abebf2c69092230028333428a90fa345a10fcb4e9c50e53f4958c61daedcc7  stockfish-16.tar.gz
e86633e1129a5e9727d137ddddba44918bb4dfb88b192659e2323ae4eafc44175c1e9c24ab1ce72dc2a05682b00bdf6aba1f1caf6b0b44ec1cef4a14ae465006  nn-5af11540bbfe.nnue
1f80c03f8b1250c74344609fddd03d8360637149bba8a0e7110e2148e289b02242f7fd1e02af2536fe008a3aa6da89505e626f4e8af04b5652647867975eb0dc  no-lto.patch
"
